define(['./module'],function(module){
	'use strict';

	module.controller('logisticsbusinessregionprice',serverlogisticsbusinessregionpricectrl);
	function serverlogisticsbusinessregionpricectrl($rootScope,$scope,$http,$location){

		$scope.shengfenshow=true;
		//通过location中的operate参数区分操作行为
		var operate=$location.search().operate;
		if(operate!=undefined&&operate=="save"){

			/**
			 *  查询所有省份
			 */
			$scope.findallshengfen=function(){
				$http({
					method:'POST',
					url:'../mall/basicregion/findRegionByType',
					data:{
						'type':"0"
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.shengfenall=data.beanlist;
						$scope.shengfenshow=true;
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});
			}

			/**
			 *  查询省份下的市
			 */
			$scope.findallshibyshengfen=function(){
				$http({
					method:'POST',
					url:'../mall/basicregion/findRegionShiByShengfen',
					data:{
						'parentid':$scope.shengfenallx.id
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.shiall=data.beanlist;
						$scope.shishow=true;
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});
			}


			/**
			 * 省份选择改变
			 */
			$scope.changeshenfen=function(){
				if($scope.shengfenallx==undefined){
					$scope.errors={
						show:true,
						msg:"请选择省份"
					}
					return false;
				}
				$scope.findallshibyshengfen();
			}

			$scope.setregions=function(){
				$scope.regions=$scope.shiallx.name;
			}

			//控制保存按钮显示
			$scope.savebtn={show:true};
			$scope.title='添加物流商运费信息';
			$scope.status="1";
			//保存数据方法
			$scope.save=function(){
				if(validate()){
					$http({
						method:'POST',
						url:'../mall/logisticsbusinessregionprice/save',
						data:{
							'logisticsname':$rootScope.logisticsname,
							'logisticsid':$rootScope.logisticsid,
							'regions':$scope.regions,
							'normalcost':$scope.normalcost,
							'normalweight':$scope.normalweight,
							'overweight':$scope.overweight,
							'overcost':$scope.overcost,
							'status':$scope.status
						}
					}).
					success(function(data,status,headers,config){
						if(data.sucflag){
							$scope.info={
									show:true,
									msg:'运费信息添加成功'
							}
							$location.path('/logisticsbusinessregionpricement').search({'operate':'find','id':$scope.logisticsid,'name':$scope.logisticsname});
						}
					}).
					error(function(data,status,headers,config){
						$scope.errors={
								show:true,
								msg:'系统异常'
						}
					});
				}
			}
		}
		
		//如果operate是edit则执行保存行为
		if(operate=='edit'){
			var id=$location.search().id;
			if(id!=undefined&&id!=""){
				$http({
					method:'POST',
					url:'../mall/logisticsbusinessregionprice/find',
					data:{
						'id':id
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.title='正在对运费信息进行编辑';
						$scope.logisticsname=data.bean.logisticsname;
						$scope.logisticsid=data.bean.logisticsid,
						$scope.regions=data.bean.regions;
						$scope.normalcost=data.bean.normalcost;
						$scope.normalweight=data.bean.normalweight;
						$scope.overweight=data.bean.overweight;
						$scope.overcost=data.bean.overcost;
						$scope.status=data.bean.status;
						$scope.updatebtn={show:true};
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});
				//更新数据方法
				$scope.update=function(){
					if(validate()){
						$http({
							method:'POST',
							url:'../mall/logisticsbusinessregionprice/update',
							data:{
								'regions':$scope.regions,
								'normalcost':$scope.normalcost,
								'normalweight':$scope.normalweight,
								'overweight':$scope.overweight,
								'overcost':$scope.overcost,
								'status':$scope.status,
								'id':id
							}
						}).
						success(function(data,status,headers,config){
							if(data.sucflag){
								$scope.info={
										show:true,
										msg:'运费信息更新成功'
								}
								$location.path('/logisticsbusinessregionpricement').search({'operate':'find','id':$scope.logisticsid,'name':$scope.logisticsname});

							}
						}).
						error(function(data,status,headers,config){
							$scope.errors={
									show:true,
									msg:'系统异常'
							}
						});
					}
				}
			}
		}
		
		//表单字段验证
		function validate(){

			if($scope.regions==undefined){
				$scope.errors={
					show:true,
					msg:"请选择配送地区"
				}
				return false;
			}
			if($scope.normalcost==undefined){
				$scope.errors={
					show:true,
					msg:"请输入标准运费"
				}
				return false;
			}
			if($scope.normalweight==undefined){
				$scope.errors={
					show:true,
					msg:"请输入标准重量"
				}
				return false;
			}
			if($scope.overweight==undefined){
				$scope.errors={
					show:true,
					msg:"请输入续重"
				}
				return false;
			}
			if($scope.overcost==undefined){
				$scope.errors={
					show:true,
					msg:"请输入续费"
				}
				return false;
			}


			return true;
		}
	}


	/*=====Begin Of Find List LogisticsBusinessT=====*/
	module.controller('logisticsbusinessregionpricement',serverlogisticsbusinessregionpricement);
	//查询列表数据
	function serverlogisticsbusinessregionpricement($rootScope,$http,$location,$compile,$scope,DTOptionsBuilder,DTColumnBuilder){
		$rootScope.logisticsid=$location.search().id;
		$rootScope.logisticsname=$location.search().name;
		$scope.title='正在查看物流商--'+$location.search().name+'--的运费列表';
		var vm=this;
		vm.message='';
		vm.someClickHandler = someClickHandler;
		vm.selected={};
		vm.selectAll=false;
		vm.toggleAll=toggleAll;
		vm.toggleOne=toggleOne;
		vm.dtInstance={};
		var titleHtml='<input type="checkbox" ng-model="showCase.selectAll" ng-click="showCase.toggleAll(showCase.selectAll,showCase.selected)">';
		vm.dtOptions=DTOptionsBuilder.newOptions().withOption('ajax',{
			type:'POST',
			url:'../mall/logisticsbusinessregionprice/findByPage',
			data:{
				'objectid':$rootScope.logisticsid
			},
			dataSrc:'data'
		})
		.withOption('processing',true)
		.withOption('paging',true)
		.withOption('serverSide',true)
		.withOption('createdRow',function(row,data,dataIndex){
			$compile(angular.element(row).contents())($scope);
		})
		.withOption('headerCallback', function(header) {
            if (!$scope.headerCompiled) {
                $scope.headerCompiled = true;
                $compile(angular.element(header).contents())($scope);
            }
        })
        .withOption('stateSave', true)
        .withOption('rowCallback',rowCallback)
		.withPaginationType('full_numbers')
		.withLanguageSource('./app/language/chinese.json')
		
		$scope.$on('handleRequest',function(){
			
		});
		
		vm.dtColumns=[
		              DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function(data,type,full,meta){
		            	  vm.selected[full.id]=false;
		            	  return '<input type="checkbox" ng-model="showCase.selected['+data.id+']" ng-click="showCase.toggleOne(showCase.selected)">';
		              }),
		              DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
			          DTColumnBuilder.newColumn('regions').withTitle('配送地区').notSortable(),
			          DTColumnBuilder.newColumn('normalcost').withTitle('运费').notSortable(),
			DTColumnBuilder.newColumn('normalweight').withTitle('重量').notSortable(),
			DTColumnBuilder.newColumn('overcost').withTitle('续重价格').notSortable(),
			DTColumnBuilder.newColumn('normalweight').withTitle('续重').notSortable(),
			DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable(),
			          DTColumnBuilder.newColumn('version').withTitle('版本号').notSortable(),
			         DTColumnBuilder.newColumn(null).withTitle('操作').notSortable().renderWith(actionHtml)];
		function actionHtml(data,type,full,meta){
			return '<button class="btn btn-warning" ng-click="edit('+data.id+')"><i class="fa fa-edit"></i></button>';
		}
		
		function reloadData(){
			var resetPaging = false;
	        vm.dtInstance.reloadData(callback, resetPaging);
		}
		function callback(data){
		}

		/**
		 * 复杂搜索刷新数据
		 */
		function searchReloadData(){
			var resetPaging=false;
			var searchParams={
				isCustomSearch:1,
				name:''
			};
			if($scope.name!=undefined){
				searchParams.name=$scope.name;
				vm.dtOptions['ajax'].data=searchParams;
				vm.dtInstance.reloadData(callback, resetPaging);
			}
		}

		/**
		 * 表格
		 */
		$scope.searchdataTableCustom=function(){
			searchReloadData();
		}


		//表格中编辑按钮
		$scope.edit=function(id){
			$location.path('/logisticsbusinessregionprice').search({'operate':'edit','id':id});
		}
		
		/**
		 * 跳转到添加商品类型和参数页面
		 */
		$scope.save=function(){
			$location.path('/logisticsbusinessregionprice').search({'operate':'save'});
		}


		
		$scope.del=function(){
			var i=0;
			var ids=[];
			angular.forEach(vm.selected, function(data,index,array){
				if(data){
					i++;
					ids.push(index);
				}
			});
			if(i==0){
				$scope.errors={
						show:true,
						msg:'请选择一条记录'
				}
			}else{
				$scope.errors={
						show:false
				}
				//批量删除数据
				var idstrs=ids.join(",");
				$http({
					method:'POST',
					url:'../mall/logisticsbusinessregionprice/del',
					data:{
						'id':idstrs
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.info={
								show:true,
								msg:'批量删除成功'
						}
						reloadData();
					}
					
				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});
			}
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll,selectedItems){
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					selectedItems[id]=selectAll;
				}
			}
		}
		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems){
			var me=this;
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					if(!selectedItems[id]){
						me.selectAll=false;
					}
				}
			}
			me.selectAll=true;
		}

		function someClickHandler(info) {
	        vm.message = info.id + ' - ' + info.name;
	    }
		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow,aData,iDisplayIndex,iDisplayIndexFull){
			$('td', nRow).unbind('click');
	        $('td', nRow).bind('click', function() {
	            $scope.$apply(function() {
	                vm.someClickHandler(aData);
	            });
	        });
	        return nRow;
		}
		
	}
});